package quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import quartz.job.MyJob1;
import quartz.job.MyJob2;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

public class Test2 {
    public static void main(String[] args) throws SchedulerException {
        test1();
    }

    /**
     * 单个job执行休息20S,总共10个worker线程,执行20次
     * 当10个线程sleep的时候trigger不会fire,但是剩下的10次fire还是执行的,等sleep的线程醒来以后立即fire.所以总共还是会执行20次
     *
     * @throws SchedulerException
     */
    public static void test1() throws SchedulerException {

        StdSchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();

        Scheduler sched = schedFact.getScheduler();

        sched.start();

        // define the job and tie it to our HelloJob class

        JobDetail job = newJob(MyJob2.class)
                .withIdentity("myJob", "group1")
                .build();


        // Trigger the job to run now, and then every 40 seconds
        Trigger trigger = newTrigger()
                .withIdentity("myTrigger", "group2")
                .startNow()
                .withSchedule(simpleSchedule()
                        .withIntervalInSeconds(1)
                        .withRepeatCount(19)
                )
                .build();


        // Tell quartz to schedule the job using our trigger
        sched.scheduleJob(job, trigger);
    }

}



